<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单备注" prop="orText">
      <el-input v-model="dataForm.orText" placeholder="订单备注"></el-input>
    </el-form-item>
    <el-form-item label="状态 （已支付1  未支付0  已取消-1  已完成2）" prop="orStatus">
      <el-input v-model="dataForm.orStatus" placeholder="状态 （已支付1  未支付0  已取消-1  已完成2）"></el-input>
    </el-form-item>
    <el-form-item label="商品状态(库存中,等待发货，已发货，已签收)" prop="orCmStatus">
      <el-input v-model="dataForm.orCmStatus" placeholder="商品状态(库存中,等待发货，已发货，已签收)"></el-input>
    </el-form-item>
    <el-form-item label="购买人" prop="urId">
      <el-input v-model="dataForm.urId" placeholder="购买人"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="cmId">
      <el-input v-model="dataForm.cmId" placeholder="商品id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orUuid: 0,
          orText: '',
          orStatus: '',
          orCmStatus: '',
          urId: '',
          cmId: ''
        },
        dataRule: {
          orText: [
            { required: true, message: '订单备注不能为空', trigger: 'blur' }
          ],
          orStatus: [
            { required: true, message: '状态 （已支付1  未支付0  已取消-1  已完成2）不能为空', trigger: 'blur' }
          ],
          orCmStatus: [
            { required: true, message: '商品状态(库存中,等待发货，已发货，已签收)不能为空', trigger: 'blur' }
          ],
          urId: [
            { required: true, message: '购买人不能为空', trigger: 'blur' }
          ],
          cmId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orUuid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orUuid) {
            this.$http({
              url: this.$http.adornUrl(`/mallgn/order/info/${this.dataForm.orUuid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orText = data.order.orText
                this.dataForm.orStatus = data.order.orStatus
                this.dataForm.orCmStatus = data.order.orCmStatus
                this.dataForm.urId = data.order.urId
                this.dataForm.cmId = data.order.cmId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mallgn/order/${!this.dataForm.orUuid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orUuid': this.dataForm.orUuid || undefined,
                'orText': this.dataForm.orText,
                'orStatus': this.dataForm.orStatus,
                'orCmStatus': this.dataForm.orCmStatus,
                'urId': this.dataForm.urId,
                'cmId': this.dataForm.cmId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
